var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    mobilephone: '',
    code: '',
    getcode:'',
    loginErrorCount: 0,
    timer: '',//定时器名字
    countDownNum: '60',//倒计时初始值
    ismove:0,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    wx.setNavigationBarTitle({
      title: "注册中心"
    })
  },
  onReady: function () {

  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理codes go here
          that.setData({
            ismove: 0,
            countDownNum:'60',
            timer:''
          })
        }
      }, 1000)
    })
  },
  getTelCode:function(){
    let that=this;
    let telphone=that.data.mobilephone;
    var strkong = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!strkong.test(telphone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        image: '/static/images/icon_error.png',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: api.getVerify,
      data: {
        telphone: that.data.telphone,
        type: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno ==0) {
          that.setData({
            getcode: res.data.msg,
            ismove:1
          });
          that.countDown();
        }
      }
    });
  },
  onShow: function () {
    // 页面显示
    //什么时候触发倒计时，就在什么地方调用这个函数

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  startRegister: function () {
    var that = this;

    if (that.data.password.length < 3 || that.data.username.length < 3) {
      wx.showModal({
        title: '错误信息',
        content: '用户名和密码不得少于3位',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.addRegister,
      data: {
        username: that.data.username,
        password: that.data.password,
        mobilephone: that.data.mobilephone,
        code:that.data.code
      },
      success: function (res) {
        if (res.data.errno == 0) {
          wx.showToast({
            title: res.data.msg,
            code: 'success',
            duration: 2000
          })
          wx.switchTab({
            url: '/pages/ucenter/index/index',
          });
        }else{
          wx.showToast({
            title: res.data.msg,
            image: '/static/images/icon_error.png',
            duration: 2000
          })
        }

      }
    });
  },
  bindUsernameInput: function (e) {
    this.setData({
      username: e.detail.value
    });
  },
  bindPasswordInput: function (e) {
    this.setData({
      password: e.detail.value
    });
  },
  bindmobilephone: function (e) {
    this.setData({
      mobilephone: e.detail.value
    });
  },
  bindCodeInput: function (e) {
    let that=this;
    let phone = that.data.mobilephone;


    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function (e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-mobilephone':
        this.setData({
          mobilephone: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})